//import fl.controls.TextInput;
//import fl.controls.Button;

import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.Responder;

class Login extends flash.display.MovieClip
{
	var nc:NetConnection;
	var input:fl.controls.TextInput;
	var log:Dynamic;
	
	public function new(nc, log)
	{
		super();
		this.nc = nc;
		this.log = log;
		
		var label = new fl.controls.Label();
		label.text = 'user name:';
		addChild(label);
		
		input = new fl.controls.TextInput();
		input.width=100;
		input.restrict="0-9a-zA-Z";
		input.x = label.x+ label.width + 10;
		addChild(input);
		
		var send_btn = new fl.controls.Button();
		send_btn.label="enter";
		send_btn.x = input.x+ input.width + 10;
		send_btn.addEventListener(flash.events.MouseEvent.CLICK, onSendBtn);
		addChild(send_btn);
	}
	function onSendBtn(event:flash.events.MouseEvent):Void
	{
		nc.call("login", new Responder(onResult, onError), input.text);
	}
	function onResult(ok:Bool)
	{
		if(ok)
			dispatchEvent(new flash.events.Event("login"));
		else
			log("name is taken");
	}
	function onError(obj:Dynamic)
	{
		log("onError");
		log(obj.level);
		log(obj.code);
		log(obj.description);
		//for(i in Reflect.fields(obj))
			//trace([i,Reflect.field(obj, i)]);
	}
	public function getUserName():String
	{
		return input.text;
	}
}
